package com.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.mapper.BookDao;
import com.book.mapper.BookFreeDao;
import com.book.model.Book;
import com.book.model.BookFree;
import com.book.result.Result;
import com.book.result.ResultEnum;
import com.book.service.BookFreeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RestController
@RequestMapping("exemptions")
public class ExemptionsController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private BookFreeService bookFreeService;

    @Autowired
    private BookFreeDao bookFreeDao;

    @Autowired
    private BookDao bookDao;

    @Test
    public void Test1(){
        System.out.println(111);
    }

    @Test
    //表示每天18:16执行一次这个方法
    @Scheduled(cron = "0 16 18 ? * MON-FRI")
    public void TestController(){
//        System.out.println(123456);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("free","2");
        List<Book> bookList = bookDao.selectList(queryWrapper);
        bookList.stream().map((x)->{
            return x;
        }).forEach((y)->{
            Date date = new Date();
            QueryWrapper<BookFree> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("book_id", y.getId());
            List<BookFree> bookFrees = bookFreeDao.selectList(queryWrapper1);
            if(date.getTime()>bookFrees.get(0).getStartTime().getTime()&&date.getTime()<bookFrees.get(0).getEndTime().getTime()){
                redisTemplate.boundHashOps("Exemptions").put(bookFrees.get(0).getBookId(),bookFrees.get(0));
                Long a = (bookFrees.get(0).getEndTime().getTime()-date.getTime());
                redisTemplate.expire("Exemptions",a,TimeUnit.MILLISECONDS);
            }
            Long bookId = bookFrees.get(0).getBookId();
            System.out.println("bookId为:"+bookId);
            if(bookFrees.get(0).getEndTime().getTime()<date.getTime()){
                QueryWrapper<Book> id = queryWrapper.eq("id", bookId);
                List<Book> books = bookDao.selectList(queryWrapper);
                Long id1 = books.get(0).getId();
                Book book = bookDao.selectById(id1);
                book.setFree("3");
                bookDao.updateById(book);
            }
            System.out.println("123456");
        });
    }

    @GetMapping("redisHost")
    public Result redisHost(){
        bookFreeService.redisHost();
        return Result.response(ResultEnum.REDIS_CG);
    }
}
